#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@AUTHOR: tyc
@VERSION: 1.0.0
@DESCRIPTION: 使用bs4爬取信息菜价
@time: 2021-07-01 23:23:23 
"""
from bs4 import BeautifulSoup
import requests
import csv

'''
find(标签，属性=值)
find_all(标签，属性=值)
'''


def write_table_csd(url):
    resp = requests.get(url)
    # html.parser指定html解析器
    page = BeautifulSoup(resp.text, "html.parser")
    # 获取表格
    table = page.find("table", attrs={"class", "hq_table"})
    # 获取表格所有行
    trs = table.find_all("tr")
    # 获取表头行 遍历
    for th in trs[0].find_all('td'):
        print(th.text)
    with open('菜价.csv', 'a+') as f:
        cs = csv.writer(f)
        for tr in trs[1:]:
            ls = []
            for td in tr.find_all("td"):
                ls.append(td.text)
            cs.writerow(ls[:-1])

str = 'http://www.xinfadi.com.cn/marketanalysis/0/list/5.shtml'
if __name__ == '__main__':
    for i in range(9):
        url = f'http://www.xinfadi.com.cn/marketanalysis/0/list/{i}.shtml'
        write_table_csd(url)
